/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.chl.da.controllerBeans;

import edu.chl.da.ejb.core.Customer;
import edu.chl.da.ejb.ctrl.IManageCustomer;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

/**
 * Controller bean managing listing, getting and removal events of Customers
 * @author Erik
 */
@ManagedBean(name = "ManageCustomersBean")
@SessionScoped
public class ManageCustomersBean {

    private List<Customer> allCustomers;
    private String selectedCustomerUsername;

    private Customer selectedCustomer;

    private boolean success = false;
    private String errorMsg;

    @EJB
    IManageCustomer imc;

    /** Creates a new instance of ManageCustomersBean */
    public ManageCustomersBean() {
        
    }

    public void loadAllCustomers() {
        this.allCustomers = imc.getCustomerList();
        
    }

    public void loadCustomer(String name){
        this.selectedCustomer = imc.getCustomer(name);
    }
    
    public String deleteCustomer() {
        Customer c = imc.getCustomer(selectedCustomerUsername);
        boolean b = imc.removeCustomer(c);
        if(b) {
            allCustomers.remove(c);
            return "/admin/manageCustomers.jsf";
        }
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Gick ej att radera kunden, kontrollera eventuella bokningarna"));
        return null;
    }

    public String navigate() {
        if (success) {
            return "/admin/manageCustomers.jsf";
        } else {
            // Adding a failure message. Possible to display all
            // messages using <h:messages> (ending s!!)
            // or just target a specific control using <h:message> (no s!).
            FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage(errorMsg));
           return null; //Same page;

        }
    }

    public List<Customer> getCustomers() {
        return allCustomers;
    }

    public void setCustomers(List<Customer> customers) {
        this.allCustomers = customers;
    }

    public String getSelectedCustomerUsername() {
        return selectedCustomerUsername;
    }

    public void setSelectedCustomerUsername(String selectedCustomerUsername) {
        this.selectedCustomerUsername = selectedCustomerUsername;
    }

    public void setSelectedCustomer(Customer seledctedCustomer) {
        this.selectedCustomer = seledctedCustomer;
    }

    public Customer getSelectedCustomer() {
        return selectedCustomer;
    }
}
